<?php

class Exporter {
	
	public static function csv($data, $filename=''){
		ob_clean();
		
		if(!$filename){
			$filename = mt_rand(100,999);
		}
		elseif(false!==strpos($_SERVER["HTTP_USER_AGENT"],'MSIE')){
//			$filename = iconv('utf-8','gb2312',$filename);
			$filename = urlencode($filename);
		}
		
		$fp = fopen('php://output', 'w');
		foreach($data as $row){                   
                    fputcsv($fp, $row);
		}
		fclose($fp);
		header('Pragma: public');
                header('Last-Modified: '.date('D, d M Y H:i:s') . ' GMT');
                header('Cache-Control: no-store, no-cache, must-revalidate');
                header('Cache-Control: pre-check=0, post-check=0, max-age=0');
                header('Content-Transfer-Encoding: binary');
                header('Content-Encoding: none');
		header('Content-type: text/csv; charset=UTF-8');
		header("Content-Disposition: attachment; filename=\"{$filename}.csv\"");
		
//		echo "$filename \n";
		echo mb_convert_encoding(ob_get_clean(), "gbk","auto");//iconv('utf-8','gb2312', ob_get_clean());
	}
	
}

?>
